package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.check.maintenance;

import cn.hutool.json.JSONObject;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.ConfigKit;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.file.FileStrUtil;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: YangYu
 * @ClassName: MaintenanceService
 * @Date: Created in 15:58 2022/8/24
 * @Description: 维保服务
 */
@Service
@Slf4j
public class MaintenanceService extends BaseService {


    /**
     * @Author YangYu
     * @Description 分区树列表
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity partitionTreeList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","partitionTreeList","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 分区新增、编辑
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity partitionAddOrUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","partitionAddOrUpdate","maintenance");
    }
    /**
     * @Author YangYu
     * @Description 分区删除
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity partitionDel(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","partitionDel","maintenance");
    }


    /**
     * @Author YangYu
     * @Description 维保设备分页列表
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity deviceListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","deviceListPage","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 设备新增、更新
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity deviceAddOrUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","deviceAddOrUpdate","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 设备删除
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity deviceDel(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","deviceDel","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 任务新增、编辑
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity taskAddOrUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","taskAddOrUpdate","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 任务删除
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity taskDel(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","taskDel","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 任务分页列表
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity taskListPage(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","taskListPage","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 任务指派
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity taskAssignment(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","taskAssignment","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 更新任务状态
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity updateState(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","updateState","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 任务书详情（维保部件详情）
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity maintenanceBookDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","maintenanceBookDetail","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 维保报告数据
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity maintenanceReport(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","maintenanceReport","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 消防设施系统列表
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity maintenanceSystemList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","maintenanceSystemList","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 设备类型列表
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity maintenanceTypeList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","maintenanceTypeList","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 设备类型列表
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity maintenanceBookList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","maintenanceBookList","maintenance");
    }

    /**
     * @Author YangYu
     * @Description 更新部件状态为异常
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity updateResult(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","updateResult","maintenance");
    }
    /**
     * @Author YangYu
     * @Description 维保报告导出
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity reportExport(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","reportExport","maintenance");
    }

    /**
     * 设备导入模板下载
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    public ResponseEntity<byte[]> getTemplate(HttpServletRequest httpServletRequest) throws Exception {
        String ip = ConfigKit.getStr("checkJava","config");
        String methodPath = getMethodPath("checkJava","getTemplate","maintenance");
        String domain = ip + methodPath;
        String filepath = "维保设备导入模板.xlsx";
        byte[] btImg = FileStrUtil.getImageFromNetByUrl(domain);
        HttpHeaders headers = new HttpHeaders();
        // 下载显示的文件名，解决中文名称乱码问题
        String downloadFielName = new String(filepath.getBytes("UTF-8"), "iso-8859-1");
        // 通知浏览器以attachment（下载方式）打开图片
        headers.setContentDispositionFormData("attachment", downloadFielName);
        // application/octet-stream ： 二进制流数据（最常见的文件下载）。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(btImg, headers, HttpStatus.CREATED);
    }

    /**
     * @Author YangYu
     * @Description 恢复分区下的设备状态为未检测
     * @Date Created in 10:07 2022/8/25
     * @Param [jsonParam]
     */

    public RespEntity updateDeviceState(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return restJava(jsonParam,"checkJava","updateDeviceState","maintenance");
    }
}
